<template>
  <div class="port-management">
    <el-row :gutter="20" class="mb-20">
      <el-col :span="24">
        <el-button type="primary" @click="showAddDialog">添加港口</el-button>
      </el-col>
    </el-row>

    <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="港口名称" width="150" />
      <el-table-column prop="freeDays" label="免费天数" width="100" />
      <el-table-column prop="feeRate" label="逾期价格" width="150" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
        <el-form-item label="港口名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入港口名称" />
        </el-form-item>
        <el-form-item label="免费天数" prop="freeDays">
          <el-input-number v-model="form.freeDays" :min="0" :max="365" placeholder="请输入免费天数" />
        </el-form-item>
        <el-form-item label="逾期价格" prop="feeRate">
          <el-input-number 
            v-model="form.feeRate" 
            :min="0" 
            :precision="2" 
            :step="0.01"
            placeholder="请输入逾期价格（元/吨/天）" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getPortList, createPort, updatePort, deletePort } from '@/api/setting'

const tableData = ref([])
const loading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('添加港口')
const formRef = ref(null)

const form = ref({
  id: null,
  name: '',
  freeDays: 0,
  feeRate: 0
})

const rules = {
  name: [{ required: true, message: '请输入港口名称', trigger: 'blur' }],
  freeDays: [{ required: true, message: '请输入免费天数', trigger: 'blur' }],
  feeRate: [{ required: true, message: '请输入逾期价格', trigger: 'blur' }]
}

const fetchData = async () => {
  loading.value = true
  try {
    const response = await getPortList()
    tableData.value = response.data
  } catch (error) {
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

const showAddDialog = () => {
  dialogTitle.value = '添加港口'
  form.value = {
    id: null,
    name: '',
    freeDays: 0,
    feeRate: 0
  }
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑港口'
  form.value = { ...row }
  dialogVisible.value = true
}

const handleDelete = (id) => {
  ElMessageBox.confirm('确认删除该港口?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await deletePort(id)
    ElMessage.success('删除成功')
    fetchData()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const submitForm = async () => {
  try {
    await formRef.value.validate()

    if (form.value.id) {
      await updatePort(form.value)
      ElMessage.success('更新成功')
    } else {
      await createPort(form.value)
      ElMessage.success('添加成功')
    }

    dialogVisible.value = false
    fetchData()
  } catch (error) {
    console.error('表单验证失败', error)
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.mb-20 {
  margin-bottom: 20px;
}
</style> 